<?php
	class girl_model extends CI_Model {

		public function __construct()
		{
			// Call the Model constructor
			parent::__construct();
		}
		
		public function girl_count() {
			return $this->db->count_all("girls");
		}		
		
		public function get_list_girl($limit, $offset, $sort_by, $sort_order){
			$sort_order = ($sort_order == 'Asc') ? 'Asc' : 'Desc';
			$sort_columns = array('id','title','image','type', 'time_update');
			$sort_by = (in_array($sort_by,$sort_columns)) ? $sort_by : 'time_update';
			$q = $this->db->select("*",FALSE)
				->from("girls")
				->limit($limit,$offset)
				->order_by($sort_by, $sort_order);
			$ret['rows'] = $q->get()->result();

			// Count Result
			$q = $this->db->select('COUNT(*) as count',FALSE)
				->from("girls");
			$tmp = $q->get()->result();
			$ret['num_rows'] = $tmp[0]->count;

			return $ret;
		}
		
		public function get_list_girl_by_time($startdate,$enddate) {
			$startdate =strtotime( date('Y/m/d 00:00:00',$startdate));
			$enddate =strtotime( date('Y/m/d 23:59:59',$enddate));			
			$this->db->select('*');
			$this->db->from('girls');
			$this->db->where('time_update >', $startdate);
			$this->db->where('time_update <=', $enddate);
			$this->db->order_by("gid", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
			return 31;
		}
		
		public function get_list_all_girl() {
			$this->db->select('*');
			$this->db->from('girls');
			$this->db->order_by("gid", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
			return "empty";
		}
				
		public function get_girl_by_id($id) {
			$q = $this->db->select('*',FALSE)
				->from("girls")
				->where('gid', $id);
			$tmp = $q->get()->result();
			if($tmp!=null)
				return $tmp[0];
			else return null;
		}
		
		public function insert_girl($data,$filename1,$filename2,$filename3,$filename4,$filename5)
		{
			//Kiểm tra trùng tên				
			if($data['name']=="")
			{
				return 4;//không tồn tại tên
			}
			else if(strlen($data['name'])<10)
			{
				return 5;
			}
			$q = $this->db->select("*",FALSE)
				->from("girls")
				->where('name', $data['name']);
			$check = $q->get()->result();
			if($check!=null)
			{
				return 13;//Đã trùng tên
			}
			else
			{
				$insert_data = array(
					'name' => $data['name'],
					'birthday' =>strtotime($data['birthday']),
					'interests' => $data['interests'],
					'talent' => $data['talent'],
					'image1' => $filename1,
					'image2' => $filename2,
					'image3' => $filename3,
					'image4' => $filename4,
					'image5' => $filename5,
					'status' => $data['status'],
					'description' => $data['description'],
					'time_update'=>time());
				$this->db->insert('girls', $insert_data);
				return 200;
			}
		}
		
		public function update_girl($data,$filename1,$filename2,$filename3,$filename4,$filename5)
		{	
			//Kiểm tra trùng tên
			$q = $this->db->select("*",FALSE)
				->from("girls")
				->where('gid !=', $data['gid'])
				->where('name', $data['name']);
			$check = $q->get()->result();
			if($check!=null)
			{
				return 13;//Đã trùng tên
			}
			else
			{
				$this->db->where('gid', $data['gid']);
				$update_data = array(
				'name' => $data['name'],
					'birthday' =>strtotime( $data['birthday']),
					'interests' => $data['interests'],
					'talent' => $data['talent'],
					'image1' => $filename1,
					'image2' => $filename2,
					'image3' => $filename3,
					'image4' => $filename4,
					'image5' => $filename5,
					'status' => $data['status'],
					'description' => $data['description'],
					'time_update'=>time());
				$this->db->update('girls', $update_data);
				return 200;
			}		
		}
		
		
		public function delete_girl($id)
		{		
			$this->db->delete('girls', array('gid' =>$id));
			return 200;
		}	
}